<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        .box {
            width: 200px;
            height: 200px;
            margin-left: 0;
            background-color: aqua;
        }
    </style>
</head>

<body>
    <div class="box"></div>
    <button class="btn">
        按钮
    </button>
</body>
<script>
    var box = document.querySelector('.box');
    var btn = document.querySelector('.btn');
    var position = 0;
    // 为btn绑定点击事件
    btn.onclick = function () {
        //设置定时器
        var set = setInterval(function () {
            position += 10;
            // 每当移动200px就会停止移动
            if (position % 200 === 0) {
                clearInterval(set) // 停止定时器
            }
            // 更新左边距 相当于移动box
            box.style.marginLeft = position + 'px';
        }, 100)
    };
</script>

</html>